package com.asia5b.a5mall.middleware.api

import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5mall.middleware.R
import com.asia5b.a5mall.middleware.utils.ToastUtil
import com.google.gson.JsonSyntaxException
import retrofit2.HttpException
import timber.log.Timber
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException

/**
 * api错误处理逻辑类，不同的数据源需要共用这个类，如果只有一种数据源可以直接在consumer后面处理；
 * Created by rui on 2018/7/31.
 */
object ApiErrorHelper {
    fun handleCommonError(throwable: Throwable) {
        try {
            Timber.e("--->>>>>>>>---throwable:" + throwable.message)
            if (throwable is IllegalStateException) {
            } else if (throwable is ConnectException) {
                ToastUtil.showToast(ResString.get(R.string.无网络连接))
            } else if (throwable is SocketTimeoutException) {
                ToastUtil.showToast(ResString.get(R.string.服务连接超时))
            } else if (throwable is HttpException) {
                val code = throwable.code()
                val s = ResString.get(R.string.服务不可用_请稍后重试)
                ToastUtil.showToast(String.format(s, code))
            } else if (throwable is UnknownHostException) {
                ToastUtil.showToast(ResString.get(R.string.未知名称或服务))
            } else if (throwable is JsonSyntaxException) {
                ToastUtil.showToast(ResString.get(R.string.错误解析异常))
            } else if (throwable is CustomThrowable) {
                if (throwable.isToast) {
                    ToastUtil.showToast(throwable.message)
                }
            } else {
                ToastUtil.showToast(throwable.message)
            }
        } catch (e: Exception) {
        }
    }
}